# -*- coding: utf-8 -*-
from ssc.items import TecentItem
import json, time, scrapy, logging

class TecentSpider(scrapy.Spider):
    name = 'tecent'
    start_urls = ['https://mma.qq.com/cgi-bin/im/online']

    def parse(self, response):
        try:

            res_jsonp = response.body.decode('utf-8')
            res_json = res_jsonp[ res_jsonp.index("(") + 1 : res_jsonp.rindex(")") ]
            res = json.loads(res_json)

            item = TecentItem()
            item['tecent_time'] = time.strftime('%Y-%m-%d %H:%M:00')
            item['tecent_value'] = res.get('c', '')
            yield item

            #ww = '腾讯人数：{}：{}\n'.format(item['tecent_time'], item['tecent_value'])

            #with open('a.json', 'a') as f:
            #    f.write(ww)

        except Exception as err:
            logging.info(err)
